﻿//========================================================================//
//      Copyright 2012, Smilodon Studio Inc.
//      All rights reserved.
//========================================================================//

/**
 *	base 
 *      @author: CYS
 */


var canvas;
var g2D;

var height;
var width;
var fullWindow = true;
var backGround; 
var time = 0;
var dtime = 0;
var startime;
var lasttime;
var balls = [];
var numBall = 5;
var ratio = 1;
var lastTouches = [];

function ball()
{
	this.x = 0;
	this.y = 0;
	this.vx = 0;
	this.vy = 0;	
	this.r = 70;
	this.c = '#000';
}

ball.prototype.draw = function(c2d)
{
	c2d.fillStyle = this.c;
	c2d.beginPath();
	c2d.arc(this.x, this.y, this.r, 0, 2*Math.PI, true);
	c2d.closePath();
	c2d.fill();

	c2d.strokeStyle = 'rgba(255, 255, 255, 0.5)';
	c2d.stroke();
}

ball.prototype.move = function()
{
	this.x += this.vx;
	this.y += this.vy;
	if( (this.y+this.r) > height )
	{
		this.y = height-this.r;
		this.vy = -this.vy*0.999;
	}
	this.vy *= 0.99;
	var h = height - this.y - this.r;
	this.vy += 0.98*20*0.08;//0.98*dtime*0.08;
}

function canvasResize()
{
	ratio = window.innerWidth/canvas.width;
	canvas.width = window.innerWidth;
	canvas.height = window.innerHeight;
	width = canvas.width;
	height = canvas.height;
	if(!g2D)g2D = canvas.getContext("2d");
	//g2D.globalAlpha = 0.85;
	//backGround = g2D.createRadialGradient(width*0.5, height, width*0.05, width*0.5, height, width*0.7);
	backGround = g2D.createLinearGradient(0, 0, 0, height);
	backGround.addColorStop(1, "#bbf");
	backGround.addColorStop(0, "#128");

}

function init() 
{
	canvas = document.getElementById('canvas2D');
	canvasResize();

	startime = Date.now();
	lasttime = Date.now();

	var touchable = 'createTouch' in document;
	if (touchable)
	{
		canvas.addEventListener('touchstart', onTouchStart, false);
		canvas.addEventListener('touchmove', onTouchMove, false);
		canvas.addEventListener('touchend', onTouchEnd, false);
	}
	else
	{
		canvas.addEventListener('mousedown', onTouchStart, false);
	}

	canvas.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );

}


var bTimeout = false;

window.onresize = function()
{
	if (fullWindow) canvasResize();
	for(var i = 0; i < balls.length; i++)
	{
		var b = balls[i];
		b.x *= ratio;
		b.y = 0;
		b.r *= ratio;
		b.vy = rnd(40.0);
	}
	bTimeout = true;


}

var lastOrientation;
var first = true;
window.ondeviceorientation =  function(e) 
{	
	if (first) 
	{
		lastOrientation = window.orientation;
		first = false;
	}
	if(lastOrientation !== window.orientation)
	{
		window.onresize();
		lastOrientation = window.orientation;
	}
}

window.onblur = function()
{
	bTimeout = true;
}


function drawScene()
{
	// draw background color 
	//g2D.clearRect(0, 0, canvas.width,canvas.height);
	//g2D.fillStyle = '#67a';
	g2D.fillStyle = backGround;
	g2D.fillRect(0, 0, canvas.width,canvas.height);

	// draw balls
	g2D.lineWidth = 4;
	for(var i = 0; i < balls.length; i++)
	{
		balls[i].move();
		balls[i].draw(g2D);
	}
}

function createBall(x)
{
	var b = new ball();
	b.c = rgba(rndi(256), rndi(256), rndi(256), rnd(0.5)+0.2);
	b.x = x;
	b.y = 0;
	b.vy = rnd(40.0);
	b.r = 0.10*width - rnd(0.07*width);
	balls.push(b);
}

function onTouchStart(event)
{
	// Prevent the browser from doing its default thing (scroll, zoom)
	event.preventDefault();
	
	if(event.clientX) // on mouse down
	{
		createBall( event.clientX );
	}
	else	// on touch star
	{
		for(var i = 0; i < event.touches.length; i++)
		{
			var bInLast = false;
			for(var j = 0; j < lastTouches.length; j++)
			{
				if(lastTouches[j].identifier === event.touches[i].identifier) bInLast = true;
			}
			if(!bInLast) createBall(event.touches[i].clientX);

		}
		lastTouches = event.touches;
	}

}

function onTouchMove(event)
{
	event.preventDefault();
}

function onTouchEnd(event)
{
	event.preventDefault();
}



function gameloop() 
{
	window.requestAnimFrame( gameloop );
	if(bTimeout)
	{
		lasttime = Date.now();
		bTimeout = false;
	}
	dtime = Date.now() - lasttime;
	time = Date.now() - startime;
	drawScene();
	lasttime = Date.now();

}


function game()
{
	init();
	gameloop();
}




////////////////////////////////////////////////////////////
//for(var i in balls)
//{
//	var b = balls[i];
//	if(b) b.draw(g2D);
//}



